package org.cllib.j2me;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;

public class Engine extends Device implements Runnable {
	private MIDlet midlet;
	private Thread t;
	public final static int FRAME_DURATION = 100;

	private String fps = "";
	private Image image;
	
	private int yOffset;
	private final static int STEP = 5;
	
	private long startTime , endTime;
	private int transform;
	
	private final static int MODE_DRAW_IMAGE = 0;
	private final static int MODE_DRAW_REGION = 1;
	private int mode;
	private int imageWidth ,imageHeight;
	public Engine(MIDlet midlet) {
		this.midlet = midlet;

		try {
			image = Image.createImage("/0.png");
			imageWidth = image.getWidth();
			imageHeight = image.getHeight();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		t = new Thread(this);
		t.start();

		setFullScreenMode(true);

		fullScreenWidth = getWidth();
		fullScreenHeight = getHeight();
		halfFullScreenWidth = fullScreenWidth >> 1;
		halfFullScreenHeight = fullScreenHeight >> 1;
	}

	public void run() {
		while (t != null) {
			long startTime = System.currentTimeMillis();

			tick();
			
			doRepaint();

			long duration = endTime - startTime;
			if (duration < FRAME_DURATION) {
				try {
					Thread.sleep(FRAME_DURATION - duration);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			if (duration > 0) {
				int integerPart = 1000 / (int)duration;
				int digitPart = 100000 / (int)duration - integerPart * 100;
				fps = String.valueOf(integerPart);
				if (digitPart > 0) {
					fps += "." + digitPart;
				}
			}
		}

		midlet.notifyDestroyed();
	}

	private void tick() {
		if (isKeyPressed(KEY_LEFT)) {
			
		}
		if (isKeyPressed(KEY_RIGHT)) {
			
		}

		if (isKeyPressed(KEY_UP)) {
			// motionY -= ADJUST_Y_STEP;
			yOffset += STEP;
		}

		if (isKeyPressed(KEY_DOWN)) {
			// motionY += ADJUST_Y_STEP;
			yOffset -= STEP;
		}

		if (isKeyPressed(KEY_FIRE)) {
		}

		if (isKeyPressed(KEY_POUND)) {
			transform = (transform + 1) % 8;
		}
		if (isKeyPressed(KEY_STAR)) {
			mode = (mode + 1) % 2;
		}
		if (isKeyPressed(KEY_0)) {
			t = null;
		}
	}

	private void doRepaint() {
		repaint();
		serviceRepaints();
	}
	
	protected void paint(Graphics g) {
		super.paint(g);

		g.setColor(COLOR_WHITE);
		g.fillRect(0, 0, fullScreenWidth, fullScreenHeight);
		
		startTime = System.currentTimeMillis();
		if(mode == MODE_DRAW_IMAGE){
			g.drawImage(image, 0, 0, Graphics.LEFT | Graphics.TOP);
			endTime = System.currentTimeMillis();
		}else if(mode == MODE_DRAW_REGION){
			g.drawRegion(image, 0, 0, imageWidth, imageHeight, transform, 0, 0, Graphics.LEFT | Graphics.TOP);
			endTime = System.currentTimeMillis();
		}
		
		g.setColor(COLOR_WHITE);
		int y = 0;
		int fh = g.getFont().getHeight();
		g.drawString(fps + ":" + (endTime - startTime), 0, y, Graphics.LEFT | Graphics.TOP);
		y+=fh;
		g.drawString(mode + "", 0, y, Graphics.LEFT | Graphics.TOP);
		y+=fh;
		g.drawString(transform + "", 0, y, Graphics.LEFT | Graphics.TOP);
		
		System.out.println("Painting ....");
	}
}
